package com.example.project.file;

import com.example.project.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Service("FileServerImpl")
public class FileServerImpl {

    @Value("${upload.root}")
    private String rootPath;
    @Value("${editorImg}")
    private String editorImg;

    public static final String FILE = "file/";
    private static final String STATE = "state";
    private static final String ERROR = "ERROR";
    private static final String SLASH = "/";

    private static final List<String> suffixs = Arrays.asList(new String[]{
            "png","jpg","gif","bmp","avi","mp4","swf","flv","rm","ram","mov","mpeg","mpg","wmv","pdf","xls","doc",
            "docx","xlsx","ppt","zip","rar","wps","rtf","gz","txt"
    });


    public Map upload(HttpServletRequest request) throws Exception {
        Map paramMap = new HashMap();
        paramMap.put(STATE,"SUCCESS");
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        // 从config.json中取得上传文件的ID
        MultipartFile upfile = multipartRequest.getFile("upfile");
        if (upfile == null) {
            throw new Exception("上传文件失败！");
        }
        String suffix = upfile.getOriginalFilename().substring(upfile.getOriginalFilename().lastIndexOf(".")+1);
        if (!suffixs.contains(suffix)){
            paramMap.put(STATE,ERROR);
            paramMap.put("msg","该类型文件没有收录进白名单！");
        } else {
            String fileName = UUIDUtils.getUUID() + "." + suffix;
            //获取项目根路径
            String rootpath = rootPath + editorImg +"/";
            //String rootpath = request.getSession().getServletContext().getRealPath(SLASH) + FILE + DateUtil.getNow(DateUtil.Y_M_D);
            String realpath = rootpath + SLASH + fileName;
            String returnpath = SLASH + FILE +  SLASH + fileName;
            checkFolder(new File(rootpath));
            if (Objects.nonNull(upfile)) {
                //获取上传的图片
                File tarfile = new File(realpath);
                if (!tarfile.exists()) {
                    try {
                        //把内存图片写入磁盘中
                        upfile.transferTo(tarfile);
                        if (!tarfile.getParentFile().exists()) {
                            tarfile.getParentFile().mkdir();
                        }
                    } catch (IOException e) {
                        paramMap.put(STATE, ERROR);
                        paramMap.put("msg", "上传失败！");
                    }
                }
                paramMap.put("url", returnpath);
                paramMap.put("title", upfile.getOriginalFilename());
                paramMap.put("original", upfile.getOriginalFilename());
                paramMap.put("type", suffix);
                paramMap.put("size", upfile.getSize());
                return paramMap;
            }
        }
        paramMap.put(STATE,ERROR);
        paramMap.put("msg","上传失败！");
        return paramMap;
    }

    private static void checkFolder(File dirPath){
        if (!dirPath.exists())
            dirPath.mkdirs();
    }
}
